package com.huihui.widget.dialog;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.huihui.R;


/**
 * Created by daimeng on 2016/11/9.
 * 自定义菊花样式
 */
public class LoadingDialogCircle extends AppCompatDialog{

    private final Context mContext;
    private ProgressBar mProgress;
    private TextView mText;
    private String mString;
    private boolean needShowText;

    public LoadingDialogCircle(Context context) {
        super(context, R.style.progress_loading_noback);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_loading_dialog);
        mProgress = (ProgressBar) findViewById(R.id.dialog_loading_progressBar);
        mText = (TextView) findViewById(R.id.dialog_loading_text);
        if(!TextUtils.isEmpty(mString)){
            mText.setText(mString);
        }

        if(needShowText){
            mText.setVisibility(View.VISIBLE);
        }
        this.setCancelable(true);
        this.setCanceledOnTouchOutside(false);
    }

    public void setText(CharSequence text){
        if(mText!=null){
            mText.setText(text);
        }else{
            mString = String.valueOf(text);
        }
    }


    public LoadingDialogCircle setText(int resourceId){
        if(mText!=null){
            mText.setText(resourceId);
        }else{
            mString = mContext.getString(resourceId);
        }
        return this;
    }


    public LoadingDialogCircle showLoadingText(boolean needShowText){
        if(needShowText){
            mText.setVisibility(TextView.VISIBLE);
        }else{
            this.needShowText = needShowText;
        }
        return this;
    }

    public void showLoading(){
        try{
            if (this != null && !this.isShowing()) {
                this.show();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public void hideLoading(){
        try{
            if (this != null && this.isShowing()) {
                this.dismiss();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
